package com.eight.cloud.client.service.payment;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.eight.cloud.client.http.HttpRequest;
import com.eight.cloud.client.util.ObjectFormat;
import com.eight.cloud.client.common.WechatPaymentMethodEnum;
import com.eight.cloud.client.config.RequestConfig;
import com.eight.cloud.client.model.dto.OrderDto;
import com.eight.cloud.client.model.dto.RefundDto;

import java.io.IOException;

/**
 * 微信支付请求
 *
 * @ClassName: WeChatPaymentRequest
 * @Author: TXC
 * @Date: 2024-11-25 16:35
 **/
public class WeChatPaymentRequest {
    private final RequestConfig config;
    private final WechatPaymentMethodEnum method;
    private final static String urlPrefix = "/pay/v1/payment/wechat/";

    public WeChatPaymentRequest(RequestConfig config, WechatPaymentMethodEnum method) {
        this.config = config;
        this.method = method;
    }

    /**
     * 预下单
     * @param order 订单信息
     * @return @Link Result
     */
    public String placeOrder(OrderDto order) throws IOException {
        if (ObjectUtil.isNull(order)) {
            return "订单信息不可为空";
        }
        if (StrUtil.isBlank(order.getOutTradeNo()) || ObjectUtil.isNull(order.getPrice()) || ObjectUtil.isNull(order.getOpenId())) {
            return "商户订单号、商品价格、用户openId不可为空";
        }
        String url = urlPrefix + method.value + "/placeOrder";
        return new HttpRequest(config).post(url, ObjectFormat.objectToStr(order));
    }


    /**
     * 退款
     *
     * @param refund      退款信息
     * @return 结果
     */
    public String refund(RefundDto refund) throws IOException {
        if (StrUtil.isBlank(refund.getOutRefundNo()) || StrUtil.isBlank(refund.getRefund()) || StrUtil.isBlank(refund.getOutTradeNo()) ||
                StrUtil.isBlank(refund.getTotal()) || StrUtil.isBlank(refund.getSubMerchantId())) {
            return "退款订单号、退款金额、订单总金额不可为空";
        }
        String url = urlPrefix + method.value + "/refund";
        return new HttpRequest(config).post(url, ObjectFormat.objectToStr(refund));
    }

    /**
     * 退款订单查询
     *
     * @param outRefundNo 退款订单号
     * @param subMerchantId   指定商户号（不可为空）
     * @return 详情
     */
    public String queryByOutRefundNo(String outRefundNo, String subMerchantId) throws IOException {
        if (StrUtil.isBlank(outRefundNo) || StrUtil.isBlank(subMerchantId)) {
            return "退款订单号和商户号不可为空";
        }
        String url = urlPrefix + method.value + "/queryByOutRefundNo";
        return new HttpRequest(config).get(url + "?" + "outRefundNo=" + outRefundNo + "&subMerchantId=" + subMerchantId);
    }

    /**
     * 根据流水号查询订单
     *
     * @param transactionId 流水号
     * @param subMerchantId   指定商户号（不可为空）
     * @return 结果
     */
    public String queryOrderById(String transactionId, String subMerchantId) throws IOException {
        if (StrUtil.isBlank(transactionId) || StrUtil.isBlank(subMerchantId)) {
            return "流水号和商户号不可为空";
        }
        String url = urlPrefix + method.value + "/queryOrderById";
        return new HttpRequest(config).get(url + "?" + "transactionId=" + transactionId + "&subMerchantId=" + subMerchantId);
    }

    /**
     * 根据商户订单号查询订单
     *
     * @param outTradeNo 商户订单id
     * @param subMerchantId   指定商户号（不可为空）
     * @return 结果
     */
    public String queryOrderByOutTradeNo(String outTradeNo, String subMerchantId) throws IOException {
        if (StrUtil.isBlank(outTradeNo) || StrUtil.isBlank(subMerchantId)) {
            return "商户订单号和商户号不可为空";
        }
        String url = urlPrefix + method.value + "/queryOrderByOutTradeNo";
        return new HttpRequest(config).get(url + "?" + "outTradeNo=" + outTradeNo + "&subMerchantId=" + subMerchantId);
    }

    /**
     * 关闭订单
     *
     * @param outTradeNo 商户订单号
     * @param subMerchantId   指定商户号（不可为空）
     * @return 结果
     */
    public String closeOrder(String outTradeNo, String subMerchantId) throws IOException {
        if (StrUtil.isBlank(outTradeNo) || StrUtil.isBlank(subMerchantId)) {
            return "商户订单号和商户号不可为空";
        }
        String url = urlPrefix + method.value + "/closeOrder";
        return new HttpRequest(config).get(url + "?" + "outTradeNo=" + outTradeNo + "&subMerchantId=" + subMerchantId);
    }

    public static class Builder {
        private RequestConfig config;
        private WechatPaymentMethodEnum method;

        public Builder setConfig(RequestConfig config) {
            this.config = config;
            return this;
        }

        public Builder setMethod(WechatPaymentMethodEnum method) {
            this.method = method;
            return this;
        }
        public WeChatPaymentRequest build() {
            return new WeChatPaymentRequest(config, method);
        }
    }
}
